﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Binarysharp.MemoryManagement;
using System.Diagnostics;

namespace LuaTest1
{
    class Program
    {
        static public MemorySharp sharp;

        static void Main(string[] args)
        {

            Console.WriteLine("Here we go");

            int pid = 0;
            foreach (Process process in Process.GetProcessesByName("Wow"))
            {
                pid = process.Id;
            }

            sharp = new MemorySharp(Process.GetProcessById(pid));

            LuaDoString("CastSpellByName(\"Stealth(rank 1)\")");

            LuaDoString("DoEmote(\"dance\")");

            LuaDoString("playerName = UnitName(\"player\");");

            //GetLocalizedText is broken until we figure out the WriteBytes accessible issue.
            //string pName = GetLocalizedText("playerName");
            //Console.WriteLine(pName);
           
            Console.WriteLine("Done");

            sharp.Dispose();

            Console.ReadLine();
        }

       static public byte[] LuaDoString(string Command)
        {            
           
            // Return Value
            byte[] tempBytes = new byte[0];

            try
            {
                //Allocate Memory For Command

                var DoStringArg_Codecave = sharp.Memory.Allocate(Encoding.UTF8.GetBytes(Command).Length + 1);

                //Execute Address
                IntPtr FrameScript_Execute = new IntPtr(0x00704CD0);

                //Write Command In Allocated Memory
                sharp.WriteString(DoStringArg_Codecave.BaseAddress, Command, false);

                var asm = new[] 
		{
			"mov ecx, " + DoStringArg_Codecave.BaseAddress,
			"mov edx, " + DoStringArg_Codecave.BaseAddress,
			"call " + FrameScript_Execute,
			"retn",    
		};

                //Inject and Execute
                sharp.Assembly.InjectAndExecute(asm);
                //Free Memory Allocated For Command
                sharp.Memory.Deallocate(DoStringArg_Codecave);
            }
            catch { }

            return tempBytes;
        }

       static public string GetLocalizedText(string Command)
       {

           uint FrameScript__GetLocalizedText = 0x703BF0;

           uint ClntObjMgrGetActivePlayerObj = 0x00468550;          

           var DoStringArg_Codecave = sharp.Memory.Allocate(Encoding.UTF8.GetBytes(Command).Length + 1);
           
           //This is broken WTF is going on here.
           //sharp.WriteBytes(DoStringArg_Codecave, Encoding.UTF8.GetBytes(Command));

           String[] asm = new String[]
            {
                "call " + ClntObjMgrGetActivePlayerObj,                            
                "mov ecx, eax",
                "push -1",
                "mov edx, " + DoStringArg_Codecave + "",
                "push edx",
                "call " + FrameScript__GetLocalizedText,
                "retn",
            };

            IntPtr inject = sharp.Assembly.InjectAndExecute(asm);

           //string sResult = Encoding.ASCII.GetString(InjectAndExecute(asm));
           string sResult = sharp.ReadString(inject);

           sharp.Memory.Deallocate(DoStringArg_Codecave);

           return sResult;
       }
         
    }
}